﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraPivotGrid;
using DevExpress.XtraPivotGrid.Customization;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using efuture.http;
using efuture.xml;
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Layout;
using DevExpress.XtraGrid.Views.Card;
using System.Xml;
using System.Xml.XPath;
using DevExpress.XtraTreeList.Columns;
using efuture.utils;

namespace efuture.vdrp
{

    public partial class frmReportDesign : DevExpress.XtraEditors.XtraForm
    {
        static String TYPE_TABLE = "table", TYPE_PIVOT = "pivot";
        static String ConfigRoot = Application.StartupPath + "\\";

        Boolean isNew = false;
        String ModelId, ModelName;
        public String ReportId, ReportParentId, ReportType, ReportCaption, ReportComments, ReportContents;
        Boolean Modified = false;

        public frmReportDesign()
        {
            InitializeComponent();
            ReportId = ReportParentId = "0";
            ReportContents = String.Empty;
            ReportType = ReportCaption = ReportComments = String.Empty;
        }

        delegate void DataSetFilledCallback(DataSet dataSet);

        void FillDataSetAsync(DataSetFilledCallback callback)
        {
            BackgroundWorker workerDataSetFiller = new BackgroundWorker();
            workerDataSetFiller.DoWork += GetDataSetAsync;
            workerDataSetFiller.RunWorkerCompleted += delegate(object s, RunWorkerCompletedEventArgs e)
            {
                callback(e.Error == null ? (DataSet)e.Result : null);
            };
            workerDataSetFiller.RunWorkerAsync();
            workerDataSetFiller.Dispose();
        }

        void GetDataSetAsync(object sender, DoWorkEventArgs e)
        {
            using (WaitDialogForm wdf = new WaitDialogForm())
            {
                wdf.Caption = "Loading data...";

                String xmlData = "";
                xmlData = "<Root><ReportInfo><DataModel>{0}</DataModel><MaxResults>10</MaxResults><Conditions><Condition><Field>BILL_DATE</Field><Op>=</Op><Value>date'2010-3-17'</Value></Condition></Conditions></ReportInfo></Root>";
                xmlData = String.Format(xmlData, ModelName);

                xmlData = HttpHelper.DoPost("/report/custom/query", xmlData, null);

                DataSet ds = new DataSet();
                ds.ReadXmlSchema(ConfigRoot + ModelName + ".xsd");
                ds.ReadXml(new StringReader(xmlData));
                e.Result = ds;
            }
        }

        static void AsyncInitializationCompleted(AsyncOperationResult result)
        {
            if (result == null)
            {
                //maybe error
            }

        }

        static void ResetGrid(GridControl gridControl1, GridView gridView1)
        {
            gridView1.ClearGrouping();
            gridView1.Columns.Clear();
            gridView1.GroupSummary.Clear();
            gridView1.FormatConditions.Clear();
            gridControl1.DataSource = null;
        }

        static void ResetPivotGrid(PivotGridControl pivotGridControl1)
        {
            pivotGridControl1.Groups.Clear();
            pivotGridControl1.Fields.Clear();
            pivotGridControl1.FormatConditions.Clear();
            pivotGridControl1.DataSource = null;
        }

        private void wizardControl1_NextClick(object sender, DevExpress.XtraWizard.WizardCommandButtonClickEventArgs e)
        {
            String xmlData = "";

            if (e.Page == this.wp1Welcome)
            {
                DataSet ds = new DataSet();
                ds.ReadXmlSchema(ConfigRoot + "ReportType.xsd");
                ds.ReadXml(ConfigRoot + "ReportType.data.xml");

                this.cardView1.DataSource = ds.Tables[0].DefaultView;
                LayoutView lv = (LayoutView)cardView1.DefaultView;

                xmlData = HttpHelper.DoGet(String.Format("report/custom/get?id={0}", ReportId), null);
                ReportType = XMLHelper.SelectSingleValue(xmlData, "/Root/Report/Type");
                if (String.IsNullOrEmpty(ReportType))
                {
                    isNew = true;
                    lv.ActiveFilterString = "";
                    ReportId = VdrpUtils.getIdentity();
                }
                else
                {
                    isNew = false;
                    lv.ActiveFilterString = String.Format("[ReportType] = '{0}'", ReportType);

                    ReportId = XMLHelper.SelectSingleValue(xmlData, "/Root/Report/Id");
                    ModelId = XMLHelper.SelectSingleValue(xmlData, "/Root/Report/ModelId");
                    ReportCaption = XMLHelper.SelectSingleValue(xmlData, "/Root/Report/Caption");
                    ReportComments = XMLHelper.SelectSingleValue(xmlData, "/Root/Report/Comments");
                    ReportContents = XMLHelper.SelectSingleValue(xmlData, "/Root/Report/Contents");

                    ColumnView cv = (ColumnView)cardView1.FocusedView;
                    if (cv != null)
                    {
                        cv.SetFocusedRowCellValue(colReportCaption, ReportCaption);
                        cv.SetFocusedRowCellValue(colReportComments, ReportComments);
                    }
                }
            }
            else if (e.Page == this.wp2ReportType)
            {
                ColumnView cv = (ColumnView)cardView1.FocusedView;

                if (String.IsNullOrEmpty(Convert.ToString(cv.GetFocusedRowCellValue(colReportCaption))))
                {
                    e.Handled = true;
                    MessageBox.Show("Report Caption is null");
                    return;
                }
                ReportCaption = Convert.ToString(cv.GetFocusedRowCellValue(colReportCaption));
                ReportComments = Convert.ToString(cv.GetFocusedRowCellValue(colReportComments));

                if (String.IsNullOrEmpty(cv.GetFocusedRowCellValue(colReportType).ToString()))
                {
                    e.Handled = true;
                    MessageBox.Show("Report Type is null");
                    return;
                }

                ReportType = cv.GetFocusedRowCellValue(colReportType).ToString();
                if (String.IsNullOrEmpty(ModelId))
                {
                    xmlData = HttpHelper.DoGet("/report/model/find", null);
                }
                else
                {
                    xmlData = HttpHelper.DoGet(String.Format("/report/model/find?ModelId={0}", ModelId), null);
                }

                DataSet ds = new DataSet();
                ds.ReadXmlSchema(ConfigRoot + "DataModel.xsd");
                ds.ReadXml(new StringReader(xmlData));
                this.edtDataModel.Properties.DataSource = ds.Tables[0];
            }
            else if (e.Page == this.wp3DataSources)
            {
                if (String.IsNullOrEmpty(this.edtDataModel.Text))
                {
                    e.Handled = true;
                    MessageBox.Show("DataSource is null");
                    return;
                }

                this.btnFormatConditions.Visible = false;

                // 
                this.pnlGridSetting.Visible = false;
                this.cbcGridOptionsView.Items.Clear();
                this.pnlPivotGridSetting.Visible = false;
                this.pnlPivotGridSetting.Controls.Clear();

                //
                this.pnlDesigner.SplitterPosition = wp4ReportDesigner.Width * 2 / 3;

                //
                this.gridControlDesigner.Visible = false;
                this.pivotGridControlDesigner.Visible = false;

                if (TYPE_TABLE.Equals(ReportType))
                {
                    // 
                    this.btnFormatConditions.Visible = true;
                    this.pnlGridSetting.Visible = true;
                    this.pnlGridSetting.Dock = DockStyle.Fill;
                    this.gridControlDesigner.Visible = true;
                    this.gridControlDesigner.Dock = System.Windows.Forms.DockStyle.Fill;

                    //
                    Object OptionsView = this.gridViewDesigner.OptionsView;
                    ArrayList arr = DevExpress.Utils.SetOptions.GetOptionNames(OptionsView);
                    foreach (Object a in arr)
                    {
                        cbcGridOptionsView.Items.Add(new CheckedListBoxItem(a, DevExpress.Utils.SetOptions.OptionValueByString(a.ToString(), OptionsView)));
                    }

                    InitGrid(treeListFields, this.gridControlDesigner, this.gridViewDesigner, AsyncInitializationCompleted);
                }
                else if (TYPE_PIVOT.Equals(ReportType))
                {
                    // 
                    this.pnlPivotGridSetting.Visible = true;
                    this.pnlPivotGridSetting.Dock = DockStyle.Fill;
                    this.pivotGridControlDesigner.Visible = true;
                    this.pivotGridControlDesigner.Dock = System.Windows.Forms.DockStyle.Fill;
                    this.pivotGridControlDesigner.OptionsCustomization.AllowFilterInCustomizationForm = true;
                    this.pivotGridControlDesigner.OptionsCustomization.AllowSortInCustomizationForm = true;
                    this.pivotGridControlDesigner.OptionsCustomization.CustomizationFormStyle = CustomizationFormStyle.Excel2007;
                    this.pivotGridControlDesigner.FieldsCustomization(this.pnlPivotGridSetting);

                    InitPivotGrid(treeListFields, this.pivotGridControlDesigner, AsyncInitializationCompleted);
                }
            }
            else if (e.Page == this.wp4ReportDesigner)
            {
                if (TYPE_TABLE.Equals(ReportType))
                {
                    InitPreview(this.gridControlDesigner);
                }
                else if (TYPE_PIVOT.Equals(ReportType))
                {
                    InitPreview(this.pivotGridControlDesigner);
                }
            }
        }

        private void InitPreview(Control control1)
        {
            if (control1 == null)
            {
                return;
            }

            //
            this.gridControlPreview.Visible = false;
            ResetGrid(this.gridControlPreview, this.gridViewPreview);

            //
            this.pivotGridControlPreview.Visible = false;
            ResetPivotGrid(this.pivotGridControlPreview);

            DataSetFilledCallback callback = delegate(DataSet dataSet)
            {
                if (dataSet == null)
                {
                    return;
                }
                InitPreviewAsync(control1, dataSet);
            };

            FillDataSetAsync(callback);
        }

        private void InitPreviewAsync(Control control1, DataSet ds)
        {
            if (typeof(GridControl) == control1.GetType())
            {
                GridControl gridControl1 = (GridControl)control1;
                GridView gridView1 = (GridView)gridControl1.MainView;
                Dictionary<String, GridColumn> ColumnMap = new Dictionary<string, GridColumn>();

                this.gridViewPreview.IndicatorWidth = 15;
                this.gridViewPreview.CustomDrawRowIndicator -= this.GridViewCustomDrawRowIndicator;
                this.gridControlPreview.Dock = DockStyle.Fill;
                this.gridControlPreview.Visible = true;

                // OptionsView
                foreach (CheckedListBoxItem i in cbcGridOptionsView.Items)
                {
                    bool b = i.CheckState == CheckState.Checked ? true : false;
                    DevExpress.Utils.SetOptions.SetOptionValueByString(i.Value.ToString(), gridViewPreview.OptionsView, b);
                }
                gridViewPreview.OptionsView.ShowGroupPanel = false;

                // 按VisibleIndex排序
                GridColumn[] Columns = (GridColumn[])new ArrayList(gridView1.Columns).ToArray(typeof(GridColumn));
                Array.Sort(Columns, delegate(GridColumn f1, GridColumn f2)
                {
                    return f1.VisibleIndex.CompareTo(f2.VisibleIndex);
                });

                this.gridControlPreview.BeginUpdate();
                this.gridViewPreview.BeginUpdate();
                foreach (GridColumn gc in Columns)
                {
                    //if (gc.Visible)
                    {
                        GridColumn gridColumn = this.gridViewPreview.Columns.Add();
                        gridColumn.Caption = gc.Caption;
                        gridColumn.FieldName = gc.FieldName;
                        gridColumn.Visible = gc.Visible;
                        //gridColumn.VisibleIndex = gc.VisibleIndex;
                        gridColumn.Width = gc.Width;
                        gridColumn.MinWidth = gc.Width;
                        gridColumn.UnboundType = gc.UnboundType;
                        gridColumn.UnboundExpression = gc.UnboundExpression;
                        gridColumn.ShowUnboundExpressionMenu = gc.ShowUnboundExpressionMenu;
                        gridColumn.GroupIndex = -1;

                        // OptionsColumn
                        gridColumn.OptionsColumn.AllowEdit = false;
                        gridColumn.OptionsColumn.AllowMove = false;
                        gridColumn.OptionsColumn.AllowSize = false;

                        // SummaryItem                 
                        gridColumn.SummaryItem.FieldName = gc.SummaryItem.FieldName;
                        gridColumn.SummaryItem.SummaryType = gc.SummaryItem.SummaryType;
                        gridColumn.SummaryItem.DisplayFormat = gc.SummaryItem.DisplayFormat;

                        // DisplayFormat
                        gridColumn.DisplayFormat.FormatType = gc.DisplayFormat.FormatType;
                        gridColumn.DisplayFormat.FormatString = gc.DisplayFormat.FormatString;

                        // AppearanceCell
                        gridColumn.AppearanceCell.Options.UseTextOptions = true;
                        gridColumn.AppearanceCell.TextOptions.HAlignment = gc.AppearanceCell.TextOptions.HAlignment;

                        ColumnMap.Add(gc.FieldName, gridColumn);
                    }
                }

                // GroupedColumns
                if (gridView1.GroupedColumns.Count == 0)
                {
                    this.gridViewPreview.IndicatorWidth = 45;
                    this.gridViewPreview.CustomDrawRowIndicator += this.GridViewCustomDrawRowIndicator;
                }
                else
                {
                    foreach (GridColumn gc in gridView1.GroupedColumns)
                    {
                        ColumnMap[gc.FieldName].Group();
                    }
                }

                // GroupSummarys
                foreach (GridSummaryItem s in gridView1.GroupSummary)
                {
                    this.gridViewPreview.GroupSummary.Add(s.SummaryType, s.FieldName, ColumnMap[s.FieldName], s.DisplayFormat);
                }

                // FormatConditions
                foreach (StyleFormatCondition s in gridView1.FormatConditions)
                {
                    StyleFormatCondition sfc = new StyleFormatCondition();
                    this.gridViewPreview.FormatConditions.Add(sfc);

                    sfc.ApplyToRow = s.ApplyToRow;
                    sfc.Column = ColumnMap[s.Column.FieldName];
                    sfc.Condition = s.Condition;
                    sfc.Expression = s.Expression;
                    sfc.Value1 = s.Value1;
                    sfc.Value2 = s.Value2;

                    sfc.Appearance.BackColor = s.Appearance.BackColor;
                    sfc.Appearance.BackColor2 = s.Appearance.BackColor2;
                    sfc.Appearance.Font = s.Appearance.Font;
                }
                this.gridViewPreview.EndUpdate();
                this.gridControlPreview.EndUpdate();
                this.gridControlPreview.DataSource = ds.Tables[0].DefaultView;
            }
            else if (typeof(PivotGridControl) == control1.GetType())
            {
                PivotGridControl pivotGrid1 = (PivotGridControl)control1;

                this.pivotGridControlPreview.Dock = DockStyle.Fill;
                this.pivotGridControlPreview.Visible = true;

                // 按AreaIndex排序
                PivotGridField[] Fields = (PivotGridField[])new ArrayList(pivotGrid1.Fields).ToArray(typeof(PivotGridField));
                Array.Sort(Fields, delegate(PivotGridField f1, PivotGridField f2)
                {
                    return f1.AreaIndex.CompareTo(f2.AreaIndex);
                });

                this.pivotGridControlPreview.BeginUpdate();
                foreach (PivotGridField f in Fields)
                {
                    //if (f.Visible)
                    {
                        PivotGridField fieldPreview = this.pivotGridControlPreview.Fields.Add();
                        fieldPreview.Caption = f.Caption;
                        fieldPreview.FieldName = f.FieldName;
                        fieldPreview.Area = f.Area;
                        fieldPreview.AreaIndex = f.AreaIndex;
                        fieldPreview.Width = f.Width;
                        fieldPreview.Visible = f.Visible;

                        fieldPreview.UnboundType = f.UnboundType;
                        fieldPreview.UnboundExpression = f.UnboundExpression;
                        fieldPreview.SummaryType = f.SummaryType;
                        fieldPreview.TotalsVisibility = f.TotalsVisibility;

                        fieldPreview.CellFormat.FormatType = f.CellFormat.FormatType;
                        fieldPreview.CellFormat.FormatString = f.CellFormat.FormatString;

                        fieldPreview.Options.ShowUnboundExpressionMenu = f.Options.ShowUnboundExpressionMenu;
                        fieldPreview.Options.AllowEdit = false;
                    }
                }
                this.pivotGridControlPreview.EndUpdate();
                this.pivotGridControlPreview.DataSource = ds.Tables[0].DefaultView;
            }
        }

        private void wizardControl1_CancelClick(object sender, CancelEventArgs e)
        {
            this.Close();
        }

        void InitPivotGrid(TreeList treeList1, PivotGridControl pivotGridControl1, AsyncCompletedHandler asyncCompletedHandler1)
        {
            using (WaitDialogForm wdf = new WaitDialogForm())
            {
                wdf.Caption = "Initialize PivotGrid...";

                ResetPivotGrid(this.pivotGridControlDesigner);

                DataSetFilledCallback callback = delegate(DataSet dataSet)
                {
                    if (dataSet == null)
                    {
                        asyncCompletedHandler1(null);
                        return;
                    }

                    InitPivotGridAsync(treeList1, pivotGridControl1);
                    pivotGridControl1.DataSource = new DataView(dataSet.Tables[0]);
                    pivotGridControl1.BestFit();
                };

                FillDataSetAsync(callback);
            }
        }

        void InitPivotGridAsync(TreeList treeList1, PivotGridControl pivotGridControl1)
        {
            treeList1.CloseEditor();

            foreach (TreeListNode node in treeList1.Nodes)
            {
                Boolean fieldVisible = Convert.ToBoolean(node[colFieldVisible]);
                String fieldSummaryType = Convert.ToString(node[colFieldSummaryType]);
                String fieldName = Convert.ToString(node[colFieldName]);
                String fieldCaption = Convert.ToString(node[colFieldCaption]);
                String fieldDataType = Convert.ToString(node[colFieldDataType]);
                String fieldFormatString = Convert.ToString(node[colFieldFormatString]);

                if (fieldVisible)
                {
                    PivotGridField field = pivotGridControl1.Fields.Add();
                    field.FieldName = fieldName;
                    field.Caption = fieldCaption;
                    field.Visible = false;
                    field.AreaIndex = -1;
                    field.TotalsVisibility = PivotTotalsVisibility.None;

                    // SummaryType
                    switch (fieldSummaryType)
                    {
                        case "Sum":
                            field.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Sum;
                            field.TotalsVisibility = PivotTotalsVisibility.AutomaticTotals;
                            break;
                        case "Average":
                            field.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Average;
                            break;
                        case "Min":
                            field.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Min;
                            break;
                        case "Max":
                            field.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Max;
                            break;
                        case "Count":
                            field.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Count;
                            break;
                        default:
                            field.SummaryType = DevExpress.Data.PivotGrid.PivotSummaryType.Count;
                            break;
                    }

                    field.Width = Convert.ToInt32(node[colFieldWidth]);
                    field.UnboundType = DevExpress.Data.UnboundColumnType.Bound;

                    // CellFormat
                    if (!String.IsNullOrEmpty(fieldFormatString))
                    {
                        field.CellFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                        field.CellFormat.FormatString = fieldFormatString;
                    }
                }
            }
            PivotGridRender.Apply(pivotGridControl1, ReportContents);
        }

        void InitGrid(TreeList treeList1, GridControl gridControl1, GridView gridView1, AsyncCompletedHandler asyncCompletedHandler1)
        {
            using (WaitDialogForm wdf = new WaitDialogForm())
            {
                wdf.Caption = "Initialize Grid...";

                ResetGrid(gridControl1, gridView1);

                DataSetFilledCallback callback = delegate(DataSet dataSet)
                {
                    if (dataSet == null)
                    {
                        asyncCompletedHandler1(null);
                        return;
                    }
                    //AsyncOperationResult
                    InitGridAsync(treeList1, gridControl1, gridView1);
                    gridControl1.DataSource = new DataView(dataSet.Tables[0]);
                    gridView1.BestFitColumns();
                };

                FillDataSetAsync(callback);
            }
        }

        void InitGridAsync(TreeList treeList1, GridControl gridControl1, GridView gridView1)
        {
            treeList1.CloseEditor();

            gridControl1.BeginUpdate();
            gridView1.BeginUpdate();
            foreach (TreeListNode node in treeList1.Nodes)
            {
                Boolean fieldVisible = Convert.ToBoolean(node[colFieldVisible]);
                String fieldSummaryType = Convert.ToString(node[colFieldSummaryType]);
                String fieldName = Convert.ToString(node[colFieldName]);
                String fieldCaption = Convert.ToString(node[colFieldCaption]);
                String fieldDataType = Convert.ToString(node[colFieldDataType]);
                String fieldFormatString = Convert.ToString(node[colFieldFormatString]);
                int width = Convert.ToInt32(node[colFieldWidth]);

                if (fieldVisible)
                {
                    GridColumn gridColumn = gridView1.Columns.Add();
                    gridColumn.FieldName = fieldName;
                    gridColumn.Caption = fieldCaption;
                    gridColumn.Visible = fieldVisible;
                    gridColumn.VisibleIndex = gridView1.Columns.Count;
                    gridColumn.Width = fieldVisible ? width : 0;

                    // OptionsColumn
                    gridColumn.OptionsColumn.AllowEdit = false;

                    // DisplayFormat
                    if (!String.IsNullOrEmpty(fieldFormatString))
                    {
                        gridColumn.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Custom;
                        gridColumn.DisplayFormat.FormatString = fieldFormatString;
                    }

                    // SummaryItem
                    String displayFormat = String.Empty;
                    DevExpress.Data.SummaryItemType summaryItemType = DevExpress.Data.SummaryItemType.None;
                    switch (fieldSummaryType)
                    {
                        case "Sum":
                            summaryItemType = DevExpress.Data.SummaryItemType.Sum;
                            displayFormat = String.Format("{0}={1}", "和", fieldFormatString);
                            break;
                        case "Average":
                            summaryItemType = DevExpress.Data.SummaryItemType.Average;
                            displayFormat = String.Format("{0}={1}", "平均", fieldFormatString);
                            break;
                        case "Min":
                            summaryItemType = DevExpress.Data.SummaryItemType.Min;
                            displayFormat = String.Format("{0}={1}", "最小值", fieldFormatString);
                            break;
                        case "Max":
                            summaryItemType = DevExpress.Data.SummaryItemType.Max;
                            displayFormat = String.Format("{0}={1}", "最大值", fieldFormatString);
                            break;
                        case "Count":
                            summaryItemType = DevExpress.Data.SummaryItemType.Count;
                            displayFormat = String.Format("{0}", "{0:n0}");
                            break;
                        default:
                            displayFormat = String.Empty;
                            break;
                    }
                    if (!String.IsNullOrEmpty(displayFormat))
                    {
                        gridColumn.Summary.Add(summaryItemType, fieldName, displayFormat);
                        gridView1.GroupSummary.Add(summaryItemType, fieldName, gridColumn, displayFormat);
                    }

                    // DataType
                    gridColumn.UnboundType = DevExpress.Data.UnboundColumnType.Bound;
                    switch (fieldDataType)
                    {
                        case "decimal":
                        case "date":
                        case "datetime":
                            gridColumn.AppearanceCell.Options.UseTextOptions = true;
                            gridColumn.AppearanceCell.TextOptions.HAlignment = DevExpress.Utils.HorzAlignment.Far;
                            break;
                        default:
                            break;
                    }
                }
            }

            gridView1.EndUpdate();
            gridControl1.EndUpdate();

            GridRender.Apply(gridView1, ReportContents);
        }

        private void GridViewCustomDrawRowIndicator(object sender, DevExpress.XtraGrid.Views.Grid.RowIndicatorCustomDrawEventArgs e)
        {
            if (e.Info.IsRowIndicator)
            {
                e.Info.DisplayText = (1 + e.RowHandle).ToString();
                e.Info.ImageIndex = -1;
            }
        }

        private void edtDataModel_EditValueChanged(object sender, EventArgs e)
        {
            if (edtDataModel.EditValue == null)
            {
                MessageBox.Show("DataSource is null");
                return;
            }

            ModelName = Convert.ToString(edtDataModel.Properties.View.GetFocusedDataRow()["ModelName"]);
            ModelId = Convert.ToString(edtDataModel.Properties.View.GetFocusedDataRow()["ModelId"]);

            treeListFields.DataSource = null;
            treeListFields.Nodes.Clear();

            String xmlData = null, url = null;
            url = String.Format("/report/model/field/find?modelId={0}", ModelId);
            xmlData = HttpHelper.DoGet(url, null);

            DataSet ds = new DataSet();
            ds.ReadXmlSchema(ConfigRoot + "DataModelField.xsd");
            ds.ReadXml(new StringReader(xmlData));
            treeListFields.DataSource = ds.Tables[0];

            if (!String.IsNullOrEmpty(ReportContents))
            {
                foreach (TreeListNode node in treeListFields.Nodes)
                {
                    node["FieldVisible"] = false;
                }

                // 
                String XPath = String.Empty;
                treeListFields.BeginUpdate();
                foreach (TreeListNode node in treeListFields.Nodes)
                {
                    String FieldName = node["FieldName"].ToString();

                    // Visible
                    XPath = "/Root/Columns/Column[FieldName='{0}']/Visible";
                    String Visible = XMLHelper.SelectSingleValue(ReportContents, String.Format(XPath, FieldName));
                    if ("1".Equals(Visible))
                    {
                        node[colFieldVisible] = true;
                    }

                    // Width
                    XPath = "/Root/Columns/Column[FieldName='{0}']/Width";
                    String Width = XMLHelper.SelectSingleValue(ReportContents, String.Format(XPath, FieldName));
                    if (!String.IsNullOrEmpty(Width))
                    {
                        node[colFieldWidth] = Int32.Parse(Width);
                    }

                    // FormatString
                    XPath = "/Root/Columns/Column[FieldName='{0}']/FormatString";
                    String FormatString = XMLHelper.SelectSingleValue(ReportContents, String.Format(XPath, FieldName));
                    if (!String.IsNullOrEmpty(FormatString))
                    {
                        node[colFieldFormatString] = FormatString;
                    }

                    // FieldSummaryType
                    XPath = "/Root/Columns/Column[FieldName='{0}']/SummaryType";
                    String SummaryType = XMLHelper.SelectSingleValue(ReportContents, String.Format(XPath, FieldName));
                    if (!"None".Equals(SummaryType))
                    {
                        node[colFieldSummaryType] = SummaryType;
                    }

                    // QueryConditions
                    XPath = "/Root/QueryConditions/QueryCondition[FieldName='{0}']/Condition";
                    String Condition = XMLHelper.SelectSingleValue(ReportContents, String.Format(XPath, FieldName));
                    if ("1".Equals(Condition))
                    {
                        node[colFieldCondition] = true;
                    }
                }
                treeListFields.EndUpdate();
            }

        }

        private void btnSetExpression_Click(object sender, EventArgs e)
        {
            using (frmExpressionEditor frm = new frmExpressionEditor())
            {
                frm.Grid = this.gridControlDesigner.Visible ? this.gridControlDesigner : null;
                frm.PivotGrid = this.pivotGridControlDesigner.Visible ? this.pivotGridControlDesigner : null;
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    // 
                }
            }
        }

        private void btnSetFormatCondition_Click(object sender, EventArgs e)
        {
            if (this.gridControlDesigner.Visible && this.gridViewDesigner.FocusedColumn != null)
            {
                using (frmFormatConditionsEditor frm = new frmFormatConditionsEditor())
                {
                    frm.GridColumn = this.gridViewDesigner.FocusedColumn;
                    if (frm.ShowDialog() == DialogResult.OK)
                    {
                        this.FindForm().Refresh();
                    }
                }
            }
        }

        private void btnExportTo_Click(object sender, EventArgs e)
        {
            if (this.gridControlPreview.Visible)
            {
                String fileName = ShowSaveFileDialog("Microsoft Excel Document", "Microsoft Excel|*.xls");
                if (fileName == string.Empty) return;

                this.gridControlPreview.ExportToXls(fileName);
            }
            else if (this.pivotGridControlPreview.Visible)
            {
                String fileName = ShowSaveFileDialog("Microsoft Excel 2007 Document", "Microsoft Excel|*.xlsx");
                if (fileName == string.Empty) return;

                this.pivotGridControlPreview.ExportToXlsx(fileName);
            }
        }

        string ShowSaveFileDialog(string title, string filter)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Title = "Export To " + title;
            dlg.FileName = ReportCaption;
            dlg.Filter = filter;
            if (dlg.ShowDialog() == DialogResult.OK) return dlg.FileName;
            return String.Empty;
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            this.gridControlPreview.DataSource = null;
            this.pivotGridControlPreview.DataSource = null;
        }

        private void cbcGridOptionsView_ItemCheck(object sender, DevExpress.XtraEditors.Controls.ItemCheckEventArgs e)
        {
            bool optionSet = e.State == CheckState.Checked ? true : false;
            string option = cbcGridOptionsView.GetDisplayItemValue(e.Index).ToString();
            DevExpress.Utils.SetOptions.SetOptionValueByString(option, this.gridViewDesigner.OptionsView, optionSet);
        }

        private void btnSaveTo_Click(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            sb.Append("<Root>");
            sb.Append("<Report>");
            sb.Append(String.Format("<Id>{0}</Id>", ReportId));
            sb.Append(String.Format("<ParentId>{0}</ParentId>", ReportParentId));
            sb.Append(String.Format("<Caption>{0}</Caption>", ReportCaption));
            sb.Append(String.Format("<Comments>{0}</Comments>", ReportComments));
            sb.Append(String.Format("<ModelId>{0}</ModelId>", ModelId));
            sb.Append(String.Format("<Type>{0}</Type>", ReportType));
            sb.Append("<Contents><![CDATA[<?xml version=\"1.0\" encoding=\"utf-8\"?><Root>");
            if (this.pivotGridControlPreview.Visible)
            {
                sb.Append("<Columns>");
                foreach (PivotGridField f in this.pivotGridControlPreview.Fields)
                {
                    sb.Append("<Column>");
                    sb.Append(String.Format("<Name>{0}</Name>", String.IsNullOrEmpty(f.Name) ? "field" + f.FieldName : f.Name));
                    sb.Append(String.Format("<Caption>{0}</Caption>", f.Caption));
                    sb.Append(String.Format("<FieldName>{0}</FieldName>", f.FieldName));
                    sb.Append(String.Format("<Visible>{0}</Visible>", "1"));
                    sb.Append(String.Format("<Area>{0}</Area>", f.Area));
                    sb.Append(String.Format("<AreaIndex>{0}</AreaIndex>", f.Visible ? f.AreaIndex : -1));
                    sb.Append(String.Format("<Width>{0}</Width>", f.Width));
                    sb.Append(String.Format("<UnboundType>{0}</UnboundType>", f.UnboundType));
                    sb.Append(String.Format("<UnboundExpression>{0}</UnboundExpression>", f.UnboundExpression));
                    sb.Append(String.Format("<SummaryType>{0}</SummaryType>", f.SummaryType));
                    sb.Append(String.Format("<TotalsVisibility>{0}</TotalsVisibility>", f.TotalsVisibility));
                    sb.Append(String.Format("<FormatType>{0}</FormatType>", f.CellFormat.FormatType));
                    sb.Append(String.Format("<FormatString>{0}</FormatString>", f.CellFormat.FormatString));
                    sb.Append("</Column>");
                }
                sb.Append("</Columns>");
            }
            else if (this.gridControlPreview.Visible)
            {
                GridView gridView1 = this.gridViewPreview;

                sb.Append("<Columns>");
                foreach (GridColumn gc in gridView1.Columns)
                {
                    if (gc.Visible)
                    {
                        sb.Append("<Column>");
                        sb.Append(String.Format("<Name>{0}</Name>", String.IsNullOrEmpty(gc.Name) ? "field" + gc.FieldName : gc.Name));
                        sb.Append(String.Format("<Caption>{0}</Caption>", gc.Caption));
                        sb.Append(String.Format("<FieldName>{0}</FieldName>", gc.FieldName));
                        sb.Append(String.Format("<Visible>{0}</Visible>", gc.Visible ? 1 : 0));
                        sb.Append(String.Format("<VisibleIndex>{0}</VisibleIndex>", gc.VisibleIndex));
                        sb.Append(String.Format("<Width>{0}</Width>", gc.Width));
                        sb.Append(String.Format("<UnboundType>{0}</UnboundType>", gc.UnboundType));
                        sb.Append(String.Format("<UnboundExpression>{0}</UnboundExpression>", gc.UnboundExpression));

                        // FormatType
                        sb.Append(String.Format("<FormatType>{0}</FormatType>", gc.DisplayFormat.FormatType));
                        if (gc.DisplayFormat.FormatType != FormatType.None)
                        {
                            sb.Append(String.Format("<FormatString>{0}</FormatString>", gc.DisplayFormat.FormatString));
                        }
                        else
                        {
                            sb.Append(String.Format("<FormatString>{0}</FormatString>", ""));
                        }

                        // SummaryType
                        sb.Append(String.Format("<SummaryType>{0}</SummaryType>", gc.SummaryItem.SummaryType));
                        if (gc.SummaryItem.SummaryType != DevExpress.Data.SummaryItemType.None)
                        {
                            sb.Append(String.Format("<SummaryDisplayFormat>{0}</SummaryDisplayFormat>", gc.SummaryItem.DisplayFormat));
                            sb.Append(String.Format("<SummaryFieldName>{0}</SummaryFieldName>", gc.SummaryItem.FieldName));
                        }
                        else
                        {
                            sb.Append(String.Format("<SummaryDisplayFormat>{0}</SummaryDisplayFormat>", ""));
                            sb.Append(String.Format("<SummaryFieldName>{0}</SummaryFieldName>", ""));
                        }

                        sb.Append("</Column>");
                    }
                }
                sb.Append("</Columns>");

                // GroupedColumns
                if (gridView1.GroupedColumns.Count > 0)
                {
                    sb.Append("<GroupedColumns>");
                    foreach (GridColumn gc in gridView1.GroupedColumns)
                    {
                        sb.Append("<GroupedColumn>");
                        sb.Append(String.Format("<FieldName>{0}</FieldName>", gc.FieldName));
                        sb.Append("</GroupedColumn>");
                    }
                    sb.Append("</GroupedColumns>");
                }

                // GroupSummary
                if (gridView1.GroupSummary.Count > 0)
                {
                    sb.Append("<GroupSummarys>");
                    foreach (GridSummaryItem gsi in gridView1.GroupSummary)
                    {
                        sb.Append("<GroupSummary>");
                        sb.Append(String.Format("<FieldName>{0}</FieldName>", gsi.FieldName));
                        sb.Append(String.Format("<SummaryType>{0}</SummaryType>", gsi.SummaryType));
                        sb.Append(String.Format("<DisplayFormat>{0}</DisplayFormat>", gsi.DisplayFormat));
                        sb.Append("</GroupSummary>");
                    }
                    sb.Append("</GroupSummarys>");
                }

                // FormatConditions
                if (gridView1.FormatConditions.Count > 0)
                {
                    sb.Append("<FormatConditions>");
                    foreach (StyleFormatCondition sfc in gridView1.FormatConditions)
                    {
                        sb.Append("<FormatCondition>");
                        sb.Append(String.Format("<FieldName>{0}</FieldName>", sfc.Column.FieldName));
                        sb.Append(String.Format("<ApplyToRow>{0}</ApplyToRow>", sfc.ApplyToRow));
                        sb.Append(String.Format("<Condition>{0}</Condition>", sfc.Condition));
                        sb.Append(String.Format("<Value1>{0}</Value1>", sfc.Value1));
                        sb.Append(String.Format("<TypeOfValue1>{0}</TypeOfValue1>", sfc.Value1 == null ? typeof(String) : sfc.Value1.GetType()));
                        sb.Append(String.Format("<Value2>{0}</Value2>", sfc.Value2));
                        sb.Append(String.Format("<TypeOfValue2>{0}</TypeOfValue2>", sfc.Value2 == null ? typeof(String) : sfc.Value2.GetType()));
                        sb.Append(String.Format("<BackColor>{0}</BackColor>", ColorTranslator.ToHtml(sfc.Appearance.BackColor)));
                        sb.Append(String.Format("<BackColor2>{0}</BackColor2>", ColorTranslator.ToHtml(sfc.Appearance.BackColor2)));
                        sb.Append(String.Format("<Font>{0}</Font>", new FontConverter().ConvertToString(sfc.Appearance.Font)));
                        sb.Append("</FormatCondition>");
                    }
                    sb.Append("</FormatConditions>");
                }
            }

            // QueryConditions
            sb.Append("<QueryConditions>");
            foreach (TreeListNode node in treeListFields.Nodes)
            {
                if ((Boolean)node[colFieldCondition])
                {
                    sb.Append("<QueryCondition>");
                    sb.Append(String.Format("<FieldName>{0}</FieldName>", node[colFieldName]));
                    sb.Append(String.Format("<Condition>{0}</Condition>", "1"));
                    sb.Append("</QueryCondition>");
                }
            }
            sb.Append("</QueryConditions>");

            sb.Append("</Root>]]></Contents>");
            sb.Append("</Report>");
            sb.Append("</Root>");

            try
            {
                if (isNew)
                {
                    HttpHelper.DoPost("/report/custom/add", sb.ToString(), null);
                }
                else
                {
                    HttpHelper.DoPost("/report/custom/update", sb.ToString(), null);
                }

                Modified = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private frmReportFilter filter = null;
        private void btnFilter_Click(object sender, EventArgs e)
        {
            //
            String xmlData = "";

            // default criteria
            HttpHelper.DoPost("/report/custom/criteria/delete", this.getDefaultCriteria(), null);
            HttpHelper.DoPost("/report/custom/criteria/add", this.getDefaultCriteria(), null);

            if (filter == null)
            {
                filter = new frmReportFilter();
            }

            filter.ReportId = ReportId;
            filter.DataModel = ModelName;
            if (filter.ShowDialog() == DialogResult.OK)
            {
                xmlData = filter.FilterString;
                xmlData = HttpHelper.DoPost("/report/custom/query", xmlData, null);

                DataSet ds = new DataSet();
                ds.ReadXmlSchema(ConfigRoot + ModelName + ".xsd");
                ds.ReadXml(new StringReader(xmlData));

                if (this.gridControlPreview.Visible)
                {
                    this.gridControlPreview.DataSource = ds.Tables[0];
                }
                if (this.pivotGridControlPreview.Visible)
                {
                    this.pivotGridControlPreview.DataSource = ds.Tables[0];
                }
            }
        }

        private void frmReportDesign_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (filter != null)
            {
                filter.Dispose();
                filter = null;
            }
        }

        private String getDefaultCriteria()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
            sb.Append("<Root>");
            sb.Append("<ReportCriteria>");
            sb.Append(String.Format("<Id>{0}</Id>", ReportId));
            sb.Append(String.Format("<CriteriaName>{0}</CriteriaName>", "默认"));
            sb.Append(String.Format("<CriteriaType>Basic,{0}</CriteriaType>", ReportId));
            sb.Append(String.Format("<ReportId>{0}</ReportId>", ReportId));
            sb.Append(String.Format("<UserId>{0}</UserId>", "0"));
            sb.Append("<Contents><![CDATA[<?xml version=\"1.0\" encoding=\"utf-8\"?><Root>");
            foreach (TreeListNode node in treeListFields.Nodes)
            {
                if ((Boolean)node["FieldCondition"])
                {
                    String Ops = (String)node["FieldOps"];
                    sb.Append("<Field>");
                    sb.Append(String.Format("<Name>{0}</Name>", node["FieldName"]));
                    sb.Append(String.Format("<Caption>{0}</Caption>", node["FieldCaption"]));
                    sb.Append(String.Format("<DataType>{0}</DataType>", node["FieldDataType"]));
                    sb.Append(String.Format("<Op>{0}</Op>", Ops.Split(',')[0]));
                    sb.Append(String.Format("<Ops>{0}</Ops>", Ops));
                    sb.Append(String.Format("<Value1>{0}</Value1>", ""));
                    sb.Append(String.Format("<Value2>{0}</Value2>", ""));
                    sb.Append("</Field>");
                }
            }
            sb.Append("</Root>]]></Contents>");
            sb.Append("</ReportCriteria>");
            sb.Append("</Root>");

            return sb.ToString();
        }

        private void frmReportDesign_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.DialogResult = this.Modified ? DialogResult.OK : DialogResult.Cancel;
        }


    }

}